
import os
import subprocess
import xml.etree.ElementTree as ET

exe_path = 'E:\\Work\\UnitTest\\Examples\\CodeAnalysis\\SourceAnalysis\\'
mingw_path = 'E:\\Work\\UnitTest\\Examples\\Host\\MinGW\\'
temp_path = 'E:\\Work\\UnitTest\\temp\\'

def analysis(src_file, config):
    cur_path = os.path.dirname(__file__)

    # 创建临时输出目录
    if not os.path.exists(temp_path+'analysis_out'):
        os.makedirs(temp_path+'analysis_out')

    base_file = os.path.basename(src_file)

    args = []
    args.append(exe_path + 'SourceAnalysis_strict.exe')
    args.append('-Source')
    args.append(src_file)
    args.append('-Include')
    args.append('include')
    args.append(mingw_path + 'lib\\gcc\\mingw32\\4.9.2\\include')
    args.append(mingw_path + 'lib\\gcc\\mingw32\\4.9.2\\include\\c++')
    args.append(mingw_path + 'lib\\gcc\\mingw32\\4.9.2\\include\\c++\\mingw32')
    args.append('-Macro')
    args.append('-Compiler')
    args.append('-AnalysisOutput')
    args.append(temp_path+'analysis_out\\'+base_file+".xml")
    args.append('-SyntaxTreeOutput')
    args.append(temp_path+'analysis_out\\'+base_file+".bin")
    args.append('-Decl_H')

    rc = subprocess.Popen(args)
    rc.wait()

    # 解析分析结果的XML文件
    tree = ET.parse(temp_path+'analysis_out\\'+base_file+".xml")
    root = tree.getroot()

    infos = {}
    infos['procedures'] = []
    
    procedures = root.findall('.//Procedure')
    for procedure in procedures:
        fun_usr = procedure.attrib['usr']

        fun_info = {}
        fun_info['basic'] = {}
        
        fun_info['basic']['name'] = procedure.attrib['name']
        fun_info['basic']['usr'] = fun_usr
        fun_info['basic']['type_str'] = procedure.attrib['type_str']
        fun_info['basic']['define'] = procedure.attrib['define']
        fun_info['basic']['start_line'] = procedure.attrib['start_line']
        fun_info['basic']['end_line'] = procedure.attrib['end_line']
        fun_info['basic']['start_col'] = procedure.attrib['start_col']
        fun_info['basic']['end_col'] = procedure.attrib['end_col']

        fun_info['params'] = []
        for param in procedure.findall('.//PramDecl'):
            paramv = {}
            paramv['usr'] = param.attrib['usr']
            paramv['type_str'] = param.attrib['type_str']
            fun_info['params'].append(paramv)

        fun_info['calls'] = []
        for call in procedure.findall('.//Call'):
            callv = {}
            callv['usr'] = call.attrib['usr']
            callv['type_str'] = call.attrib['type_str']
            fun_info['calls'].append(callv)
        
        fun_info['vars'] = []
        vars = procedure.findall('.//Var')
        for var in vars:
            varv = {}
            varv['usr'] = var.attrib['usr']
            varv['type_str'] = var.attrib['type_str']
            varv['io'] = var.attrib['io']
            fun_info['vars'].append(varv)

        infos['procedures'].append(fun_info)

    return infos

    return {
        'procedures': [
            {
                'basic': {
                    'name': 'fun3',
                    'usr': 'c:@F@fun3',
                    'type_str': 'int(int *)',
                    'define': 'false',
                    'start_line': '2',
                    'end_line': '2',
                    'start_col': '1',
                    'end_col': '17'
                },
                'params':[
                    {
                        'usr': 'c:loop.c@11@F@fun3@p',
                        'type_str': 'int *'
                    }
                ],
                'calls': [
                    {
                        'usr': 'c:@F@fun2',
                        'type_str': 'fun2'
                    }
                ],
                'vars': [
                    {
                        'usr': 'c:@gg',
                        'type_str': 'int',
                        'io': 'in'
                    },
                    {
                        'usr': 'c:@gg',
                        'type_str': 'int',
                        'io': 'out'
                    }
                ]
            }
        ]
    }